package com.vincent.javacourse.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.vincent.javacourse.entity.Teacher;
import com.vincent.javacourse.mapper.TeacherMapper;
import com.vincent.javacourse.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Security;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-12-24
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    //教师注册
    public Map<String, Object> createTeacher(Teacher teacher) {
        String confirmCode = IdUtil.getSnowflake(1, 1).nextIdStr();
        String salt = RandomUtil.randomString(6);
        String md5Pwd = SecureUtil.md5(teacher.getPassword() + salt);
        LocalDateTime ldt = LocalDateTime.now().plusDays(1);
        teacher.setTeachername(teacher.getTeachername());
        teacher.setTel(teacher.getTel());
        teacher.setLoginname(teacher.getLoginname());
        teacher.setEmail(teacher.getEmail());
        teacher.setSalt(salt);
        teacher.setPassword(md5Pwd);
        teacher.setActivationtime(ldt);
        teacher.setConfirmcode(confirmCode);
        teacher.setIsvalid((byte) 0);
        int result = teacherMapper.insert(teacher);
        Map<String, Object> resultMap = new HashMap<>();
        if (result > 0) {
            resultMap.put("code", 200);
            resultMap.put("message", "创建成功");
        } else {
            resultMap.put("code", 400);
            resultMap.put("message", "创建失败");
        }
        return resultMap;
    }

    //修改密码
    public Map<String, Object> updateTeacher(Teacher teacher) {
        String salt = RandomUtil.randomString(6);
        String md5Pwd = SecureUtil.md5(teacher.getPassword() + salt);
        teacher = teacherMapper.selectByTel(teacher.getTel());
        int result = teacherMapper.updateByTel(teacher.getTel(), md5Pwd, salt);
        Map<String, Object> resultMap = new HashMap<>();
        if (result > 0) {
            resultMap.put("code", 200);
            resultMap.put("message", "修改成功");
        } else {
            resultMap.put("code", 400);
            resultMap.put("message", "修改失败");
        }
        return resultMap;
    }

    //验证登录
    public Map<String, Object> loginTeacher(Teacher teacher) {
        Teacher teacher1 = teacherMapper.selectByTel(teacher.getTel());
        String md5Pwd = SecureUtil.md5(teacher.getPassword() + teacher1.getSalt());
        Map<String, Object> resultMap = new HashMap<>();

        if (md5Pwd.equals(teacher1.getPassword())) {
            resultMap.put("code", 200);
            resultMap.put("message", "登录成功！");
        } else {
            resultMap.put("code", 400);
            resultMap.put("message", "登录失败！");
        }

        return resultMap;
    }
}
